//
//  UserViewController.m
//  Fengly_Nuo_Movie
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 Fengly_Nuo. All rights reserved.
//

#import "UserViewController.h"
#import "LogInViewController.h"
#import "Fengly_nuo_SQLDataBaseHandle.h"
#import "UserCollectViewController.h"
#import "SDImageCache.h"

// MARK:VIEW_CENTER_X:屏中心x轴
#define VIEW_CENTER_X self.view.center.x

// MARK:VIEW_CENTER_Y:屏中心y轴
#define VIEW_CENTER_Y self.view.center.y

// MARK:HEIGHT:屏高
#define HEIGHT self.view.frame.size.height

// MARK:WIDTH:屏宽
#define WIDTH self.view.frame.size.width

@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate, ChangeRightButtonTitleDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIBarButtonItem *rightLogInButtonItem;
@property (nonatomic, retain) UITableViewCell *cell;

@property (nonatomic, retain) NSArray *arrData;

@end

@implementation UserViewController

- (void)dealloc {
    [_arryCollectActivity release];
    [_cell release];
    [_rightLogInButtonItem release];
    [_arrData release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title = @"我的";
    self.rightLogInButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightLogInButtonItemAction:)];
    [_rightLogInButtonItem setTintColor:[UIColor blackColor]];
    self.navigationItem.rightBarButtonItem = _rightLogInButtonItem;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
    imgView.image = [UIImage imageNamed:@"123.jpg"];
    _tableView.tableHeaderView = imgView;
    
    self.arrData = [NSArray arrayWithObjects:@"我的活动", @"我的电影", @"清除缓存", nil];
}




#pragma mark - 点击事件
- (void)rightLogInButtonItemAction:(UIBarButtonItem *)buttonItem {
    // 如果右边按钮标题为 登录 执行跳转到登录页面
    if ([_rightLogInButtonItem.title isEqualToString:@"登录"]) {
        LogInViewController *logInVC = [[LogInViewController alloc] init];
        UINavigationController *logInNav = [[UINavigationController alloc] initWithRootViewController:logInVC];
        [self presentViewController:logInNav animated:YES completion:^{
            
        }];
        logInVC.delegate = self;
        [logInVC release];
        [logInNav release];
    }
    
    // 如果右边按钮标题为 注销  执行注销事件
    if ([_rightLogInButtonItem.title isEqualToString:@"注销"]) {
        UIAlertController *cancelAlert = [UIAlertController alertControllerWithTitle:@"注销提示" message:@"你确定要注销" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            _rightLogInButtonItem.title = @"登录";
            UIAlertController *seccesAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注销成功" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:seccesAlert animated:YES completion:nil];
            [self performSelector:@selector(time) withObject:self afterDelay:0.5];
        }];
        UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [cancelAlert addAction:yesAction];
        [cancelAlert addAction:noAction];
        [self presentViewController:cancelAlert animated:YES completion:nil];
    }
}



// 执行自定义协议方法
- (void)changeRightButtonTitle:(NSString *)titleString {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:(UIAlertControllerStyleAlert)];
    [self presentViewController:alert animated:YES completion:^{
        _rightLogInButtonItem.title = titleString;
        [self performSelector:@selector(time) withObject:self afterDelay:0.5];
    }];
}




#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    _cell.textLabel.text = _arrData[indexPath.row];
    _cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
    return _cell;
}

- (void)time {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCollectViewController *userCollectVC = [[UserCollectViewController alloc] init];
    userCollectVC.title = @"我的活动";
    // 如果未登录 将不能进入收藏列表进行相应的操作
    if ([_rightLogInButtonItem.title isEqualToString:@"登录"]) {
        // 提示信息
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:^{
            [self performSelector:@selector(time) withObject:self afterDelay:0.5];
        }];
        // 如果已登录且当点击 我的活动 时执行
    } else if (indexPath.row == 0) {
        // 获取收藏数组
        NSArray *arr = [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] selectTable];
        // 属性传值
        userCollectVC.arrData = [NSMutableArray arrayWithArray:arr];
        
        // 给bool赋值 用于收藏列表 删除哪个数据库列表中数据的 判断
        userCollectVC.b = NO;
        
        // 页面跳转
        [self.navigationController pushViewController:userCollectVC animated:YES];
        // 如果已登录且当点击 我的电影 时执行
    } else if (indexPath.row == 1) {
        NSArray *arr = [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] selectMovieTable];
        userCollectVC.arrData = [NSMutableArray arrayWithArray:arr];
        
        userCollectVC.b = YES;
        
        [self.navigationController pushViewController:userCollectVC animated:YES];
        // 清除缓存
    } else if (indexPath.row == 2) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"清除缓存" preferredStyle:(UIAlertControllerStyleActionSheet)];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            // 清除缓存数据库调用
            [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] deleteAll];
            // 清除缓存
            [[SDImageCache sharedImageCache] clearDisk];
            
        }];
        
        [alert addAction:cancel];
        [alert addAction:confirm];
        [self presentViewController:alert animated:YES completion:nil];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
